package com.efesco.crm.crm;

import java.text.SimpleDateFormat;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.efesco.common.Tools;

/**
 * employeeMViewModel
 */
public class employeeMViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters,ParameterSet outparameters)
      throws Exception {
	  Dataset ds =getDataset("dataset_employee");
	  DBStatement dbs = new DBStatement();
	  String id1=ds.getString("ID_CARD_NUM").toLowerCase();
	  String id2=Tools.IdCardTransfer(id1).toLowerCase();
	  dbs.setSql("select employee_id from s_employee where id_card_type=1 and lower(id_card_num)in('"+id1+"','"+id2+"')");
	  java.util.List list=dbs.queryForList();
	  if(list.size()>0){
		  outparameters.setString("emplpueeId", "fail");
		  return;
	  }
	  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	  if(ds.getCurrent().getState()==Record.STATE_MODIFIED)
	  com.efesco.common.CommUtil.UpdateData(ds, "s_employee","EMPLOYEE_ID");
	  else{
	  String empId=com.efesco.common.CommUtil.getNewId("general");
	  String sql = "insert into s_employee(employee_id,employee_code,employee_name,employee_gender,employee_birthday,id_card_num,id_card_type,employee_status,is_valid)values("
				+ empId
				+ ",EMPLOYEE_CODE.nextVal,'"
				+ ds.getString("EMPLOYEE_NAME")
				+ "','"
				+ ds.getValue("EMPLOYEE_GENDER")
				+ "',to_date('"
				+ df.format(ds.getDate("EMPLOYEE_BIRTHDAY"))
				+ "','yyyy-MM-dd'),'"
				+ ds.getValue("ID_CARD_NUM")
				+ "','"
				+ ds.getValue("ID_CARD_TYPE") + "',1,1)";
		dbs.setSql(sql);
	  dbs.execute();
	  outparameters.setString("emplpueeId", empId);
	  }
  }

}
